package com.example.budget3.settings;

import java.text.DecimalFormat;
import com.example.budget3.R;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceFragment;
 
public class UserSettingFragment extends PreferenceFragment implements
											OnSharedPreferenceChangeListener{ 

    private ListPreference currencyPreference;
    private NumPadDialogPreference budgetPreference;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
 
        addPreferencesFromResource(R.xml.preferences);
        
        currencyPreference = (ListPreference) getPreferenceScreen()
                .findPreference(UserSettingActivity.KEY_CURRENCY_PREFERENCE);
        
        budgetPreference = (NumPadDialogPreference) getPreferenceScreen()
                .findPreference(UserSettingActivity.KEY_BUDGET_PREFERENCE);
         
    }

    
    @Override
    public void onResume() {
        super.onResume();
        // Setup the initial values
        currencyPreference.setSummary("Current currency is "
                + getPreferenceScreen().getSharedPreferences().getString(UserSettingActivity.KEY_CURRENCY_PREFERENCE, ""));
        
        
        double budget = Double.valueOf(getPreferenceScreen().getSharedPreferences().getString(UserSettingActivity.KEY_BUDGET_PREFERENCE, "0"));
    	String df = new DecimalFormat("#0.00").format(budget);
    	
    	budgetPreference.setSummary("Current budget is " + getPreferenceScreen().getSharedPreferences().getString(UserSettingActivity.KEY_CURRENCY_PREFERENCE, "")
                + df);
    	
    	
    	budgetPreference.setOnPreferenceChangeListener(
                new Preference.OnPreferenceChangeListener() {

                @Override
                public boolean onPreferenceChange(Preference preference, Object newValue) {
                    return Float.valueOf((String) newValue) > 0;
                }

            });
    	
        // Set up a listener whenever a key changes
        getPreferenceScreen().getSharedPreferences()
                .registerOnSharedPreferenceChangeListener(this);

    }
    
    @Override
    public void onPause() {
        super.onPause();
        // Unregister the listener whenever a key changes
        getPreferenceScreen().getSharedPreferences()
                .unregisterOnSharedPreferenceChangeListener(this);
    }
    
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
            String key) {
        // Let's do something when a preference value changes
    	if (key.equals(UserSettingActivity.KEY_CURRENCY_PREFERENCE)) {
            currencyPreference.setSummary("Current currency is "
                    + sharedPreferences.getString(key, ""));
            double budget = Double.valueOf(sharedPreferences.getString(UserSettingActivity.KEY_BUDGET_PREFERENCE, "0"));
        	String df = new DecimalFormat("#0.00").format(budget);
        	
        	budgetPreference.setSummary("Current budget is " + getPreferenceScreen().getSharedPreferences().getString(UserSettingActivity.KEY_CURRENCY_PREFERENCE, "")
                    + df);
            
            
        }else if (key.equals(UserSettingActivity.KEY_BUDGET_PREFERENCE)) {
        	double budget = Double.valueOf(sharedPreferences.getString(key, ""));
        	String df = new DecimalFormat("#0.00").format(budget);
        	
        	budgetPreference.setSummary("Current budget is " + getPreferenceScreen().getSharedPreferences().getString(UserSettingActivity.KEY_CURRENCY_PREFERENCE, "")
                    + df);
        }
    }
    
}
